package com.neutech.demo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class UserDaoJdbcImpl implements UserDao{
    static Connection conn;
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentOperatingSystem", "root", "261317");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    static PreparedStatement ps = null;
    static ResultSet rs = null;
    @Override
    public void insert(User user){
       try{
           ps = conn.prepareStatement("insert into user(name,pwd,email,birthday,money) values (?,?,?,?,?)");
       } catch (SQLException e) {
           e.printStackTrace();
       }
       try {
           ps.setString(1,user.getName());
           ps.setString(2,user.getPwd());
           ps.setString(3,user.getEmail());
           ps.setDate(4,new java.sql.Date(user.getBirthday().getTime()));
           ps.setInt(5,user.getMoney());
           ps.executeUpdate();
       }catch (SQLException e){
           e.printStackTrace();
       }
    }

    @Override
    public void update(User user){
        try {
            ps = conn.prepareStatement("update user set pwd=?,email=?,birthday=?,money=? where name=?");
            ps.setString(1,user.getName());
            ps.setString(2,user.getPwd());
            ps.setString(3,user.getEmail());
            ps.setDate(4,new java.sql.Date(user.getBirthday().getTime()));
            ps.setInt(5,user.getMoney());
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    @Override
    public void delete(User user){
        try{
            ps = conn.prepareStatement("delete from user where id = ?");
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    @Override
    public void query() {
        try {
            ps = conn.prepareStatement("select * from user");
            rs=ps.executeQuery();
            while (rs.next()){
                User user = new User();
                user.setName(rs.getString("name"));
                user.setPwd(rs.getString("pwd"));
                user.setEmail(rs.getString("email"));
                user.setBirthday(rs.getDate("birthday"));
                user.setMoney(rs.getInt("money"));
                System.out.println(user);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
        @Override
    public User getUser(String name) {
        User user=null;
        try {
            ps=conn.prepareStatement("select * from user where name=?");
            ps.setString(1,name);
            rs=ps.executeQuery();
            if (rs.next()){
                user=new User();
                user.setName(rs.getString("name"));
                user.setPwd(rs.getString("password"));
                user.setEmail(rs.getString("email"));
                user.setBirthday(rs.getDate("birthday"));
                user.setMoney(rs.getInt("money"));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public void transfer(String from, String to, int money){
        try {
            conn.setAutoCommit(false);
            ps=conn.prepareStatement("select money from user where name = ?");
            ps.setString(1,from);
            rs=ps.executeQuery();
            if (rs.next()){
                int fromMoney=rs.getInt("money");
                if (fromMoney<money){
                    throw new SQLException("余额不足!");
                }
            }
            ps=conn.prepareStatement("update user set money=money-? where name=?");
            ps.setInt(1,money);
            ps.setString(2,from);
            ps.executeUpdate();
            ps=conn.prepareStatement("update user set money=money+? where name=?");
            ps.setInt(1,money);
            ps.setString(2,to);
            ps.executeUpdate();
            System.out.println("转账成功!");
        } catch (SQLException e) {
            try {
                System.out.println(e.getMessage());
                System.out.println("转账失败!");
                conn.rollback();
            } catch (SQLException ex) {
                System.out.println(ex.getMessage());
            }
        }
        try {
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
